<?php
header('Content-Type: text/plain');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();
$action = $_POST['action'];
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'save':
      save($myPdo);
      break;
    case 'remove':
      remove($myPdo);
      break;
    case 'add_plugin':
      add_plugin($myPdo);
      break;
    case 'remove_plugin':
      remove_plugin($myPdo);
      break;
    case 'update_plugin':
      update_plugin($myPdo);
      break;
    case 'delimg':
      delimg($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function save($myPdo) {
  if (!isset($_SESSION[ADMIN])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  if ($q->is_default == 1) {
    $myPdo->exec('UPDATE pz2_page SET is_default = 0 WHERE sys_id = ' . $q->sys_id);
  }
  if (isset($q->id)) {
    if ($q->parent_id != $q->_parent_id) {
      $cond_vals = new stdClass();
      $cond_vals->c = 't.parent_id = :v1';
      $cond_vals->v = array(':v1' => $q->parent_id);
      $opts = new stdClass();
      $opts->select_expr = 'MAX(t.order) AS max';
      $q->order = DbUtils::get($myPdo, 'pz2_page', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d[0]->max + 1;
    }
    DbUtils::update($myPdo, 'pz2_page', $q);
  } else {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.parent_id = :v1';
    $cond_vals->v = array(':v1' => $q->parent_id);
    $opts = new stdClass();
    $opts->select_expr = 'MAX(t.order) AS max';
    $q->order = DbUtils::get($myPdo, 'pz2_page', $cond_vals, NULL, NULL, NULL, NULL, $opts)->d[0]->max + 1;
    DbUtils::add($myPdo, 'pz2_page', $q);
    $q->id = $myPdo->lastInsertId();
  }
  echo json_encode($q);
}

function remove($myPdo) {
  if (!isset($_SESSION[ADMIN])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  if ($q->is_default == 1) {
    $tmp = DbUtils::get($myPdo, 'pz2_page', NULL, NULL, NULL, 1, 1, NULL)->d;
    if (count($tmp) > 0) {
      $tmp[0]->is_default = 1;
      DbUtils::update($myPdo, 'pz2_page', $tmp[0]);
    }
  }
  DbUtils::delete($myPdo, 'pz2_page', $q);
  $cond_vals = new stdClass();
  $cond_vals->c = 'parent_id = :v1';
  $cond_vals->v = array(':v1' => $q->id);
  DbUtils::deleteByCond($myPdo, 'pz2_page', $cond_vals);
  $cond_vals = new stdClass();
  $cond_vals->c = 'page_id = :v1';
  $cond_vals->v = array(':v1' => $q->id);
  DbUtils::deleteByCond($myPdo, 'pz2_plugin_instance', $cond_vals);
  echo json_encode(array());
}

function add_plugin($myPdo) {
  if (!isset($_SESSION[ADMIN])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  DbUtils::add($myPdo, 'pz2_plugin_instance', $q);
  echo json_encode(array());
}

function remove_plugin($myPdo) {
  if (!isset($_SESSION[ADMIN])) {
    return;
  }
  $q = MiscUtils::getParam('q', 0);
  $cond_vals = new stdClass();
  $cond_vals->c = 'id = :v1';
  $cond_vals->v = array(':v1' => $q);
  DbUtils::deleteByCond($myPdo, 'pz2_plugin_instance', $cond_vals);
  echo json_encode(array());
}

function update_plugin($myPdo) {
  if (!isset($_SESSION[ADMIN])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  DbUtils::update($myPdo, 'pz2_plugin_instance', $q);
  echo json_encode(array());
}

function delimg($myPdo) {
  if (!isset($_SESSION[ADMIN])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  if (file_exists('../../../' . $q->_path)) {
    @unlink('../../../' . $q->_path);
  }
  DbUtils::update($myPdo, 'pz2_page', $q);
  echo json_encode($q);
}
?>